//
//  HomeMoreC.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/7/9.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "HomeMoreC.h"
#import "FirstOneCollectionCell.h"
#import "FirDetailController.h"
@interface HomeMoreC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UITableView *listTableview;

@property(nonatomic,strong)UICollectionView *listCollectionview;
@property(nonatomic,strong)NSArray *contentArray;

@end

@implementation HomeMoreC

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *onedic = self.moreArray[0];
    self.contentArray = onedic[@"SON"];
    [self creatTableviewMethod];
    [self crearCollectionview];
    
    
}


-(void)creatTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 100*kJLXWidthScale, kScreenHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    if (kScreenHeight == 812) {
        self.listTableview.sd_layout
        .bottomSpaceToView(self.view, 34);
    }
        self.listTableview.dataSource = self;
        self.listTableview.delegate = self;
        [self.listTableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"onecell"];
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    
}

-(void)crearCollectionview{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.listCollectionview = [[ UICollectionView alloc ] initWithFrame:CGRectMake(100*kJLXWidthScale, 0, kScreenWidth - 100*kJLXWidthScale, kScreenHeight) collectionViewLayout :layout];
    [self.view addSubview:self.listCollectionview];
    if (kScreenHeight == 812) {
        self.listCollectionview.sd_layout
        .bottomSpaceToView(self.view, 34);
    }
    self.listCollectionview.delegate = self;
    self.listCollectionview.dataSource = self;
    self.listCollectionview.backgroundColor = [UIColor whiteColor];
    [self.listCollectionview registerClass :[ FirstOneCollectionCell class ] forCellWithReuseIdentifier : @"twocell" ];

    [self.view addSubview:self.listCollectionview];
    
}


#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.moreArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;//cell选中以后的颜色
    NSDictionary *dic = self.moreArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",dic[@"CATEGORY_NAME"]];
    if (kScreenWidth == 320) {
        cell.textLabel.font = [UIFont systemFontOfSize:13];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame] ;
    cell.selectedBackgroundView.backgroundColor = [UIColor orangeColor];
    //字体颜色
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//    [cell.textLabel setTextColor:[UIColor whiteColor]];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.moreArray[indexPath.row];
    self.contentArray = dic[@"SON"];
    [self.listCollectionview reloadData];
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

#pragma mark--------------------UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource
//cell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.contentArray.count;
}
//
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FirstOneCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"twocell" forIndexPath:indexPath];
    NSDictionary *dic = self.contentArray[indexPath.row];
    cell.titImageview.frame = CGRectMake(0, 0,(kScreenWidth - 100*kJLXWidthScale) / 3 -20*kJLXWidthScale, (kScreenWidth - 100*kJLXWidthScale) / 3 - 30*kJLXWidthScale);
    cell.titImageview.center = cell.contentView.center;
    NSURL *url = [NSURL URLWithString:[MethodClass returnUrlSetStrWSDLURL:dic[@"CATEGORY_IMG"]]];
    [cell.titImageview sd_setImageWithURL:url];
    cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"CATEGORY_NAME"]];
    
    
    return cell;
    
}
#pragma mark-------------------布局代理
//设置每个cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth - 100*kJLXWidthScale) / 3 -1, (kScreenWidth - 100*kJLXWidthScale-10*kJLXWidthScale)/3);
}
//设置分区缩进
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(8*kJLXWidthScale, 0, 8*kJLXWidthScale, 0);
}
//设置每个分区之间最小的行间距和列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10*kJLXWidthScale;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
#pragma mark--------------------------cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FirDetailController *detailVC = [[FirDetailController alloc]init];
    NSDictionary *conDic = self.contentArray[indexPath.row];
    detailVC.navigationItem.title = conDic[@"CATEGORY_NAME"];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/goods/app/list.do?GOODS_CATEGORY=%@",conDic[@"CATEGORY_ID"]];
    detailVC.urlStr = urlstr;
    detailVC.dic = conDic;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
